<% title t("views.moderations.actions.meta.title2", title: @moderatable.title) %>

<%= javascript_include_tag "commentModPage", defer: true %>

<% cache(release_adjusted_cache_key("mod-styling")) do %>
  <style>
    <%= Rails.application.assets["moderators.css"].to_s.html_safe %>
  </style>
<% end %>

<% if flash[:error].present? %>
  <div class="crayons-banner crayons-banner--error" role="alert">
    <%= flash[:error] %>
  </div>
<% end %>

<div class="container mod-container" style="text-align: center">
  <h2><a href="<%= @moderatable.path %>" rel="nofollow"><%= @moderatable.title %></a></h2>
  <h3><a href="<%= @moderatable.user.path %>">@<%= @moderatable.user.username %></a></h3>
  <% if @moderatable.class.name == "Article" &&
       (Tag.where(requires_approval: true).pluck(:name) & @moderatable.decorate.cached_tag_list_array).any? &&
       (current_user.any_admin? || @tag_moderator_tags.any? { |tag| @moderatable.tag_list.include?(tag.name) }) %>
    <%= form_tag "/article_approvals", method: "post" do %>
      <%= hidden_field_tag(:id, @moderatable.id) %>
      <% if @moderatable.approved %>
        <%= hidden_field_tag(:approved, false) %>
        <button class="crayons-btn crayons-btn--secondary approval-button approval-removal"><%= t("views.moderations.actions.approve.remove") %></button>
      <% else %>
        <%= hidden_field_tag(:approved, true) %>
        <button class="crayons-btn approval-button"><%= t("views.moderations.actions.approve.add") %></button>
      <% end %>
    <% end %>
    <hr />
  <% end %>

  <div id="content-mod-actions" class="flex justify-around">
  <button class="reaction-button <%= Reaction.cached_any_reactions_for?(@moderatable, current_user, "thumbsup") ? "reacted" : "" %>"
          data-reactable-id="<%= @moderatable.id %>"
          data-category="thumbsup"
          data-reactable-type="<%= @moderatable.class.name %>">
    <div class="reaction-button-circle">
      <%= crayons_icon_tag("twemoji/thumb-up", native: true, width: 32, height: 32, title: t("views.moderations.actions.thumb_up")) %>
      <%= crayons_icon_tag("checkmark", class: "reaction-checkmark", title: t("views.moderations.actions.checkmark")) %>
    </div>
    <span class="vote-text"><%= t("views.moderations.actions.vote_up") %></span>
  </button>
    <button class="reaction-button <%= Reaction.cached_any_reactions_for?(@moderatable, current_user, "thumbsdown") ? "reacted" : "" %>"
            data-reactable-id="<%= @moderatable.id %>"
            data-category="thumbsdown"
            data-reactable-type="<%= @moderatable.class.name %>">
      <div class="reaction-button-circle">
        <%= crayons_icon_tag("twemoji/thumb-down", native: true, width: 32, height: 32, title: t("views.moderations.actions.thumb_down")) %>
        <%= crayons_icon_tag(:checkmark, class: "reaction-checkmark", title: t("views.moderations.actions.checkmark")) %>
      </div>
      <span class="vote-text"><%= t("views.moderations.actions.vote_down") %></span>
    </button>
    <button class="reaction-button <%= Reaction.cached_any_reactions_for?(@moderatable, current_user, "vomit") ? "reacted" : "" %>"
            data-reactable-id="<%= @moderatable.id %>"
            data-category="vomit"
            data-reactable-type="<%= @moderatable.class.name %>">
      <div class="reaction-button-circle">
        <%= crayons_icon_tag("twemoji/flag", native: true, width: 32, height: 32, title: t("views.moderations.actions.flag")) %>
        <%= crayons_icon_tag(:checkmark, class: "reaction-checkmark", title: t("views.moderations.actions.checkmark")) %>
      </div>
      <span class="vote-text"><%= t("views.moderations.actions.flag_to_admins") %></span>
    </button>
  </div>

  <% if current_user.super_admin? && @moderatable.class.name == "Article" %>
    <h3> <a href="<%= @moderatable.path %>/edit"><%= t("views.moderations.actions.edit.post") %></a> |
      <a href="/resource_admin/articles/<%= @moderatable.id %>" data-no-instant><%= t("views.moderations.actions.edit.resource_admin") %></a> |
      <a href="<%= admin_article_path(@moderatable.id) %>" data-no-instant><%= t("views.moderations.actions.edit.article_admin") %></a></h3>
  <% elsif current_user.super_admin? && @moderatable.class.name == "Comment" %>
    <h3> <a href="/admin/content_manager/comments/<%= @moderatable.id %>" data-no-instant><%= t("views.moderations.actions.edit.comment_admin") %></a> |
      <a href="<%= admin_user_path(@moderatable.user_id) %>" data-no-instant><%= t("views.moderations.actions.edit.user_admin") %></a></h3>
  <% end %>
  <p>
    <b style="font-size:1em"><%= t("views.moderations.actions.hint.subtitle") %></b>
  </p>
  <p>
    <%= t("views.moderations.actions.hint.desc_html") %>
  </p>
  <div class="tag-mod-form" style="display: flex; flex-direction: column; align-items: center;">
    <h2><%= t("views.moderations.actions.abusive.subtitle_html", user: link_to(@moderatable.user.username, @moderatable.user.path)) %></h2>
    <button class="reaction-button <%= Reaction.cached_any_reactions_for?(@moderatable.user, current_user, "vomit") ? "reacted" : "" %>"
            data-reactable-id="<%= @moderatable.user.id %>"
            data-category="vomit"
            data-reactable-type="<%= @moderatable.user.class.name %>">
      <div class="reaction-button-circle">
        <%= crayons_icon_tag("twemoji/flag", native: true, title: t("views.moderations.actions.flag")) %>
        <%= crayons_icon_tag(:checkmark, class: "reaction-checkmark", title: t("views.moderations.actions.checkmark")) %>
      </div>
      <span class="vote-text"><%= t("views.moderations.actions.flag_user", username: @moderatable.user.username) %></span>
    </button>
    <p>
      <%= t("views.moderations.actions.abusive.desc_html") %>
    </p>
  </div>
  <% if current_user.any_admin? && @moderatable.class.name == "Comment" && !@moderatable.deleted %>
    <div class="crayons-card crayons-card--secondary p-4 flex items-center">
      <div class="fw-medium flex-1" style="text-align: left;">
        <p class="fw-bold fs-xl"><%= t("views.moderations.actions.delete.subtitle") %></p>
      </div>
      <div class="flex">
        <%= button_to t("views.moderations.actions.delete.button"), comment_admin_delete_path(@moderatable.id), method: :patch, class: "crayons-btn crayons-btn--secondary ml-2" %>
      </div>
    </div>
  <% end %>
  <% if @hidden_comments.present? %>
    <div class="tag-mod-form">
      <h2><%= t("views.moderations.actions.hidden.subtitle") %></h2>
      <form>
        <ul id="hidden-comments">
        <% @hidden_comments.each do |comment| %>
          <li>
            <a href="<%= comment.path %>"><%= t("views.moderations.actions.hidden.by", user: comment.user.username) %></a>
          </li>
        <% end %>
        </ul>
      </form>
    </div>
  <% end %>
  <% if @allowed_to_adjust %>
    <div class="tag-mod-form">
      <h2><%= t("views.moderations.actions.tag.subtitle") %></h2>
      <%= form_for(@tag_adjustment) do |f| %>
        <%= t("views.moderations.actions.tag.live_html", tags: @moderatable.tag_list) %><br>
        <% if @tag_adjustment.errors.any? %>
          <div id="error_explanation">
            <h2><%= t("views.moderations.actions.errors", count: @tag_adjustment.errors.count) %></h2>
            <ul>
              <% @tag_adjustment.errors.full_messages.each do |message| %>
                <li><%= message %></li>
              <% end %>
            </ul>
          </div>
        <% end %>
        <%= f.hidden_field :article_id, value: @moderatable.id %>
        <% if current_user.any_admin? || @tag_moderator_tags.any? { |tag| @moderatable.tag_list.include?(tag.name) } %>
          <div class="tag-mod-addition-radio">
            <%= f.radio_button :adjustment_type, "removal", required: true %>
            <%= f.label :adjustment_type, t("views.moderations.actions.tag.remove"), value: "removal" %>
          </div>
        <% end %>
        <% if @moderatable.tag_list.size < 4 %>
          <div class="tag-mod-addition-radio">
            <%= f.radio_button :adjustment_type, "addition", required: true %>
            <%= f.label :adjustment_type, t("views.moderations.actions.tag.add"), value: "addition" %>
          </div>
        <% end %>
        <% if current_user.super_admin? %>
          <%= f.text_field :tag_name, placeholder: t("views.moderations.actions.tag.tag_name"), required: true %>
        <% else %>
          <%= f.select :tag_name, @tag_moderator_tags, { prompt: t("views.moderations.actions.tag.select") }, required: true %>
        <% end %>
        <%= f.text_area :reason_for_adjustment, placeholder: t("views.moderations.actions.tag.reason"), required: true %>
        <%= f.submit t("views.moderations.actions.tag.submit") %>
      <% end %>
      <% if @adjustments.present? %>
        <% @adjustments.each do |adjustment| %>
          <% if current_user.any_admin? || adjustment.user_id == current_user.id %>
            <%= form_for(adjustment, url: "/tag_adjustments/#{adjustment.id}",
                                     html: { method: :delete, onsubmit: "return confirm('#{t('views.moderations.actions.tag.undo.confirm', type: t("views.moderations.actions.tag.undo.type.#{adjustment.adjustment_type}"), tag: adjustment.tag_name)}')" }) do |f| %>
              <%= t("views.moderations.actions.tag.#{adjustment.adjustment_type == 'removal' ? 'removed' : 'added'}_html", tag: adjustment.tag_name) %>
              <%= f.submit t("views.moderations.actions.tag.undo.button"), id: "undo" %>
            <% end %>
          <% end %>
        <% end %>
      <% end %>
    </div>
  <% end %>

  <% if @moderatable.class.name == "Article" %>
    <div class="tag-mod-form">
      <% @rating_vote = RatingVote.where(article_id: @moderatable.id, user_id: current_user.id).first %>
      <% rating_hash = { "Novice" => 1, "Beginner" => 2, "Mid-level" => 3, "Advanced" => 4, "Expert" => 5 } %>

      <h2><%= t("views.moderations.actions.experience.heading2") %></h2>

      <% rating_hash.each do |rating_name, rating_level| %>
      <button
        value="<%= rating_level %>"
        name="rating_vote_<%= rating_level %>"
        id="js__rating__vote__<%= rating_level %>"
        class="level-rating-button article-mod-page <%= "selected" if @rating_vote&.rating.to_d == rating_level.to_d %>"
        data-user-id="<%= current_user.id %>"
        data-article-id="<%= @moderatable.id %>"
        data-group="experience_level"
        style="margin: 0.5rem 0;">
        <div class="level-rating-content">
          <span class="level-rating-number"><%= t("views.moderations.actions.experience.from", lvl: rating_level) %></span>
          <span class="level-rating-text"><%= t("views.moderations.actions.experience.level.#{rating_name}") %></span>
        </div>
      </button>
      <% end %>
      <p>
        <%= t("views.moderations.actions.experience.desc_html") %>
      </p>
    </div>
  <% end %>
</div>
